package com.ziyun.job.modules.job.task.seller;

import com.ziyun.erp.modules.bonusRule.service.SellerBonusRuleService;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import com.ziyun.erp.modules.order.vo.OrderBonusVo;
import io.renren.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 商户奖励金结算
 * @author gaosheng
 * @date 2018-11-14 17:48
 */
@Slf4j
@Component("synSellerOrderRewardTask")
public class SynSellerOrderRewardTask {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private SellerBonusRuleService sellerBonusRuleService;

    public void synSellerOrderReward(){
        log.info("====================seller order reward task start====================");
        /**
         * 2018-11-17 00:00:00至2018-12-08 23:59:59 奖励金取随机立减金额
         * 2018-12-09 00:00:00 之后的取订单奖励金额
         */
        List<OrderBonusVo> orderInfoList = orderInfoService.selectSellerOrderList();

        for (OrderBonusVo orderBonusVo : orderInfoList){
            String orderId = orderBonusVo.getOrderId();
            Integer bonusType = orderBonusVo.getBonusType();
            log.info("==================== row：order = {}", orderBonusVo.toString());
            try {
                // 默认奖励金倍数
                int multiple = 1;
                if (bonusType.intValue() == 0){
                    Date createtime = orderBonusVo.getCreatetime();
                    R timeBonusRule = sellerBonusRuleService.getTimeBonusRule(createtime);
                    int code = (int) timeBonusRule.get("code");
                    String msg = (String) timeBonusRule.get("msg");
                    log.info("==================== rule exception：orderId = {}, code = {}, msg = {}", orderId, code, msg);
                    if (code == 0){
                        multiple = (int) timeBonusRule.get("multiple");
                    }else if (code == 101){
                        continue;
                    }
                    log.info("==================== rule multiple：orderId = {}, multiple = {}", orderId, multiple);
                }
                erpOrderService.sellerOrderReward(orderBonusVo, multiple);
            }catch (Exception e){
                e.printStackTrace();
                log.error("==================== exception：orderId = {}, msg = {}", orderId, e.getMessage());
            }
        }

        log.info("====================seller order reward task end====================");
    }

}
